package com.example.controller;

import com.example.pojo.dao.PostIndexRepository;
import com.example.pojo.entity.PostIndex;
import com.example.service.PostsIndexService;
import com.example.service.PostsService;
import com.example.vo.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.elasticsearch.core.SearchHit;
import org.springframework.data.elasticsearch.core.SearchHits;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author 小岁
 * @version 2023/10/24 23:43
 */
@RestController
@RequestMapping("PostsSearch")
public class PostIndexController {
    @Autowired
    private PostsService postsService;

    @Autowired
    private PostIndexRepository postIndexRepository;

    @Autowired
    private PostsIndexService postsIndexService;

//    @GetMapping("/search/{posName}")
//    public Result getByPosName(@PathVariable("posName")String posName){
//        System.out.println("==================");
//        List<SearchHit<PostIndex>> postIndexList = postIndexRepository.findByPosName(posName);
//        System.out.println(postIndexList);
//        if (postIndexList.isEmpty()){
//            return new Result(500,"查询失败");
//        }else {
//            return new Result(200,"查询成功",postIndexList);
//        }
//    }
//    @GetMapping("/search/{posName}")
//    public Result getByPosNames(@PathVariable("posName")String posName){
//        System.out.println("==================");
//
//        SearchHits<PostIndex> postIndexList = postsIndexService.findByPosNameOrCompanyNames(posName,posName);
//        System.out.println(postIndexList+"aaabbbccc");
//         if (postIndexList.getTotalHits() <= 0){
//            return new Result(500,"查询失败");
//        }else {
//            return new Result(200,"查询成功",postIndexList);
//        }
//    }
    @GetMapping("/search/{posName}")
    public Result getByPosName(@PathVariable("posName")String posName){
        System.out.println("==================");

        List<SearchHit<PostIndex>> postIndexList = postIndexRepository.findByPosNameOrCompanyNameOrPosDescription(posName,posName,posName);
        System.out.println(postIndexList+"查到了");
        if (postIndexList.isEmpty()){
            return new Result(500,"查询失败");
        }else {
            return new Result(200,"查询成功",postIndexList);
        }
    }
    @GetMapping("/select")
    public Result getPosName(Integer jrId,String posName){
        System.out.println("=========aaaabbbb");
        List<SearchHit<PostIndex>> postIndexList = postIndexRepository.findByJrIdAndPosNameOrPosDescriptionOrPosKeyword(jrId,posName,posName,posName);
        System.out.println(posName);
        System.out.println(jrId);
        System.out.println(postIndexList);
        if (postIndexList.isEmpty()){
            return new Result(500,"查询失败");
        }else {
            return new Result(200,"查询成功",postIndexList);
        }
    }

    @GetMapping("/findById")
    public Result getPosId(String id,String posDescription){
        System.out.println("+++++++===========");
        List<SearchHit<PostIndex>> postIndexList = postIndexRepository.findByIdOrPosDescription(id,posDescription);
        System.out.println(id);
        System.out.println(postIndexList);
        if (postIndexList.isEmpty()){
            return new Result(500,"查询失败");
        }else {
            return new Result(200,"查询成功",postIndexList);
        }
    }
}
